package com.itheima.topic5;

import org.junit.Test;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class MyTest {
    @Test
    public void testW() throws IOException {
        //定义一个Properties对象
        Properties properties = new Properties();
        //获得DemoClass字节码对象
        Class<DemoClass> cls = DemoClass.class;
        System.out.println(cls.getName());
        //装入属性
        properties.setProperty("className",cls.getName());
        //写入 config.properties文件
        properties.store(new FileWriter("src/config.properties"),"classConfig");
    }

    @Test
    public void testR() throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //定义一个Properties对象
        Properties properties = new Properties();
        //加载properties对象
        properties.load(new FileReader("src/config.properties"));
        //根据键取值
        String className = properties.getProperty("className");
        //获取Class对象
        Class<?> cls = Class.forName(className);
        //获取方法对象
        Method run = cls.getDeclaredMethod("run");
        //获取对象，运行方法
        Object o = cls.getDeclaredConstructor().newInstance();
        run.invoke(o);

    }

}


